import { createRouter, createWebHistory } from 'vue-router'
import { useUserStore } from '@/stores/user'

// 路由懒加载
const Layout = () => import('@/layout/index.vue')
const Login = () => import('@/views/Login.vue')
const Register = () => import('@/views/Register.vue')
const Dashboard = () => import('@/views/Dashboard.vue')
const LiteratureList = () => import('@/views/literature/LiteratureList.vue')
const LiteratureDetail = () => import('@/views/literature/LiteratureDetail.vue')
const LiteratureUpload = () => import('@/views/literature/LiteratureUpload.vue')
const FavoriteList = () => import('@/views/literature/FavoriteList.vue')
const Visualization = () => import('@/views/visualization/Index.vue')
const Search = () => import('@/views/search/Index.vue')
const Profile = () => import('@/views/user/Profile.vue')
const Settings = () => import('@/views/system/Settings.vue')
const Help = () => import('@/views/help/Index.vue')
const About = () => import('@/views/About.vue')

const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes: [
    {
      path: '/login',
      name: 'Login',
      component: Login,
      meta: { title: '登录', requiresAuth: false }
    },
    {
      path: '/register',
      name: 'Register',
      component: Register,
      meta: { title: '注册', requiresAuth: false }
    },
    {
      path: '/',
      component: Layout,
      redirect: '/dashboard',
      meta: { requiresAuth: true },
      children: [
        {
          path: 'dashboard',
          name: 'Dashboard',
          component: Dashboard,
          meta: { title: '首页', icon: 'House' }
        },
        {
          path: 'literature',
          name: 'Literature',
          meta: { title: '文献管理', icon: 'Document' },
          children: [
            {
              path: 'list',
              name: 'LiteratureList',
              component: LiteratureList,
              meta: { title: '文献列表' }
            },
            {
              path: 'upload',
              name: 'LiteratureUpload',
              component: LiteratureUpload,
              meta: { title: '上传文献' }
            },
            {
              path: 'detail/:id',
              name: 'LiteratureDetail',
              component: LiteratureDetail,
              meta: { title: '文献详情', hidden: true }
            },
            {
              path: 'favorites',
              name: 'FavoriteList',
              component: FavoriteList,
              meta: { title: '我的收藏' }
            }
          ]
        },
        {
          path: 'search',
          name: 'Search',
          component: Search,
          meta: { title: '文献搜索', icon: 'Search' }
        },
        {
          path: 'visualization',
          name: 'Visualization',
          component: Visualization,
          meta: { title: '数据可视化', icon: 'PieChart' }
        },
        {
          path: 'profile',
          name: 'Profile',
          component: Profile,
          meta: { title: '个人中心', icon: 'User' }
        },
        {
          path: 'settings',
          name: 'Settings',
          component: Settings,
          meta: { title: '系统设置', icon: 'Setting' }
        },
        {
          path: 'help',
          name: 'Help',
          component: Help,
          meta: { title: '帮助中心', icon: 'Help' }
        },
        {
          path: 'about',
          name: 'About',
          component: About,
          meta: { title: '关于我们', icon: 'InfoFilled' }
        }
      ]
    },
    {
      path: '/:pathMatch(.*)*',
      name: 'NotFound',
      redirect: '/dashboard'
    }
  ]
})

// 路由守卫
router.beforeEach(async (to, from, next) => {
  const userStore = useUserStore()
  
  // 设置页面标题
  if (to.meta.title) {
    document.title = `${to.meta.title} - 医学文献管理系统`
  }
  
  // 检查是否需要认证
  if (to.meta.requiresAuth !== false) {
    if (!userStore.isAuthenticated) {
      // 尝试初始化用户状态
      if (userStore.token) {
        try {
          await userStore.initUser()
          next()
        } catch (error) {
          next('/login')
        }
      } else {
        next('/login')
      }
    } else {
      next()
    }
  } else {
    // 已登录用户访问登录/注册页面，重定向到首页
    if (userStore.isAuthenticated && (to.path === '/login' || to.path === '/register')) {
      next('/dashboard')
    } else {
      next()
    }
  }
})

export default router